//------------------------------------------------------------------------
// Abstract: Define of class SQLiteDBHelper
//
// Authoring: XiaoHanxiong
//
// Time: 2014.03.14
//
// Static test:
//
// Version history:
//		Ver_1.0	: 2014.06.14	XiaoHanxiong
//------------------------------------------------------------------------
#ifndef __Database_SQLiteDBHelper_h_
#define __Database_SQLiteDBHelper_h_

#include "DBAdministrator.h"
#include "DBStructure.h"

namespace Database
{	
	// Declare the SQLite database class
	class SQLiteDB;

	// Define of class SQLiteDBHelper
	class _DATABASE SQLiteDBHelper : public IDatabaseHelper
	{
	public:
		// Constructor
		SQLiteDBHelper(SQLiteDB* database);
		// Destructor
		virtual ~SQLiteDBHelper();

	protected:
		// Check the initialize pointer
		void CheckIDatabase();

	/* Basic Operation on database */
	public:
		// Create and open a database(file)
		bool Create(const char* database);
		// Connect to a database(file)
		bool Connect(const char* database);
		// Open a database(memory,temporary file)
		bool Open();
		// Close a database
		void Close();
	public:
		// Execute SQL
		bool ExecuteSQL(const char* sql);
		// Prepare a SQL statement object
		bool PrepareSQL(const char* sql);
		// Get last error information of SQLite
		const char* GetLastError();

	/* I/O Operation on database */
	public:
		// Prepare for insert record
		bool PrepareInsert(Table& table);
		// Insert the data into database
		bool Insert();
		// Set Write max number
		void SetWMax(int max);
		// Prepare for select record
		bool PrepareQuery(Table& table);
		// If exist next record
		bool Next();
	public:
		// Insert records into the table
		bool ExecuteInsert(Table& table);
		// Query the table records
		bool ExecuteQuery(Table& table, int num_max);

	/* Operation on database table */
	public:
		// Is the table exist
		bool IsTblExist(string table_name);
		// Drop a table
		bool Drop(string table_name);
		// Truncate table
		bool Truncate(string table_name);
		// Get table record count
		bool GetRecordCount(string table_name, int& count);
		// Get the table field information
		bool GetTableFields(string table_name, Table& table);
		// Create a table
		bool CreateTable(Table& table);
		// Rename table name
		bool Rename(string& old_name, string& new_name);
		// Create table index
		bool CreateIndex();//null

	/* DLL type operation */
	public:
		// Commit
		bool Commit();
		// Rollback
		bool Rollback();
		// Begin/End an Transaction
		bool Transaction(bool flag);
		// Set synchronous operation with disk(0 off, 1 normal, 2 full)
		bool Synchronous(int mode);

	/* Set/Get data by index from database */
	public:
		// Set field data by index
		inline bool Set(int index, bool value);
		inline bool Set(int index, char value);
		inline bool Set(int index, unsigned char value);
		inline bool Set(int index, short value);
		inline bool Set(int index, unsigned short value);
		inline bool Set(int index, int value);
		inline bool Set(int index, unsigned int value);
		inline bool Set(int index, __int64 value);
		inline bool Set(int index, unsigned __int64 value);
		inline bool Set(int index, float value);
		inline bool Set(int index, double value);
		inline bool Set(int index, string& value);
		inline bool Set(int index, const void* blob, int size);
		inline bool Set(int index); // set null
	public:
		// Get field data by index
		inline bool Get(int index, bool& value);
		inline bool Get(int index, char& value);
		inline bool Get(int index, unsigned char& value);
		inline bool Get(int index, short& value);
		inline bool Get(int index, unsigned short& value);
		inline bool Get(int index, int& value);
		inline bool Get(int index, unsigned int& value);
		inline bool Get(int index, __int64& value);
		inline bool Get(int index, unsigned __int64& value);
		inline bool Get(int index, float& value);
		inline bool Get(int index, double& value);
		inline bool Get(int index, string& value);
		inline bool Get(int index, const void*& blob, int& size);


	protected:
		// The pointer to the database
		SQLiteDB* m_database;
		// Max number of Read/Write
		unsigned int m_nMax;
		// Current index of Read/Write
		unsigned int m_curIndex;
	};
}

#endif